<?php

namespace Biz\module\Behaviors\Order;

use Biz\pockets\constInterface\ActivityInfo;
use Biz\pockets\constInterface\OrderDiscountInfo;
use Biz\pockets\constInterface\OrderLimitConfig;
use Doraemon\pockets\constInterface\AppType;
use Doraemon\tools\HhzClient;

abstract class OrderFormatBase implements OrderLimitConfig
{

    /**
     * sku数量列表
     * @var array
     */
    protected $skuNumList = [];

    /**
     * 计算限时抢的商品
     * @var array
     */
    protected $limitRobGoods = [];
    /**
     * 计算套餐活动的商品
     * @var array
     */
    protected $mealGoods = [];
    /**
     * 计算团购活动的商品
     * @var array
     */
    protected $tuanGoods = [];
    /**
     * 计算满包邮的商品
     * @var array
     */
    protected $fullShippingGoods = [];
    /**
     * 计算店铺优惠券的商品
     * @var array
     */
    protected $shopCouponGoods = [];
    /**
     * 计算平台优惠券的商品
     * @var array
     */
    protected $couponGoods = [];
    /**
     * 计算使用积分的商品
     * @var array
     */
    protected $integralGoods = [];

    /**
     * 计算使用红包的商品
     * @var array
     */
    protected $redPacketGoods = [];

    /**
     * 计算运费的商品
     * @var array
     */
    protected $shippingGoods = [];

    abstract public function computeConfirmOrder($uid, $list, $areaCode, $isToken = false, $sCouponInfo = [], $couponId = 0, $integral = 0, $mealId = 0);

    public function __construct($skuNumList)
    {
        $this->skuNumList = $skuNumList;
    }

    protected function useConfig($skuInfo, $activityType, $appType, $discountType = [])
    {
        //设置计算运费的商品
        $this->setGoods($skuInfo);

        //设置配置中参加的活动
        foreach (self::LIMIT_CONFIG[$activityType][self::LIMIT_ACTIVITY] as $useType) {
            if (in_array($appType, self::LIMIT_CONFIG[$activityType][self::LIMIT_APP_TYPE])) {
                $this->useActivity($skuInfo, $useType);
            }
        }

        //设置中允许使用的优惠类型
        foreach (self::LIMIT_CONFIG[$activityType][self::LIMIT_DISCOUNT] as $useType) {
            if (in_array($useType, $discountType)) {
                $this->useDiscount($skuInfo, $useType);
            }
        }

        //todo
        foreach (self::LIMIT_CONFIG[$activityType][self::LIMIT_APP_TYPE] as $useType) {
            if ($useType == $appType) {
                $this->useAppType($skuInfo, $useType);
            }
        }
    }

    private function useActivity($skuInfo, $activityType)
    {
        switch ($activityType) {
            case ActivityInfo::ACTIVITY_TYPE_LIMIT_TIME_ROB:
                $this->setLimitTimeRobGoods($skuInfo);
                break;
            case ActivityInfo::ACTIVITY_TYPE_TUAN:
                $this->setTuanGoods($skuInfo);
                break;
            case ActivityInfo::ACTIVITY_TYPE_MEAL:
                $this->setMealGoods($skuInfo);
                break;
            case ActivityInfo::ACTIVITY_TYPE_NEWCOMER_EXCLUSIVE:
                $this->setNewCommerGoods($skuInfo);
                break;
            case ActivityInfo::ACTIVITY_TYPE_WEEK_PURCHASE:
                $this->setWeekGoods($skuInfo);
                break;
            case ActivityInfo::ACTIVITY_TYPE_FULL_SHIPPING:
                $this->setFullShippingGoods($skuInfo);
                break;
            case ActivityInfo::ACTIVITY_TYPE_PINTUAN:
                $this->setPinTuanGoods($skuInfo);
                break;
            case ActivityInfo::ACTIVITY_TYPE_NORMAL:
                //
                break;
        }
    }

    private function useDiscount($skuInfo, $discountType)
    {
        switch ($discountType) {
            case OrderDiscountInfo::DISCOUNT_SHOP_COUPON:
                if (version_compare(HhzClient::getClientVersion(), '3.6.0', '>=')) {
                    $this->setShopCouponGoods($skuInfo);
                }
                break;
            case OrderDiscountInfo::DISCOUNT_HHZ_COUPON:
                $this->setHhzCouponGoods($skuInfo);
                break;
            case OrderDiscountInfo::DISCOUNT_HHZ_INTEGRAL:
                $this->setIntegralGoods($skuInfo);
                break;
            case OrderDiscountInfo::DISCOUNT_HHZ_RED_PACKET:
                $this->setRedPacketGoods($skuInfo);
                break;
        }
    }

    private function useAppType($skuInfo, $appType)
    {
        switch ($appType) {
            case AppType::APPTYPE_ANDROID:
            case AppType::APPTYPE_IOS:
                break;
            case AppType::APPTYPE_MINIPROGRAM:
                break;
            case AppType::APPTYPE_TUAN_MINIPROGRAM:
                break;
            case AppType::APPTYPE_PARAGRAPH_MINIPROGRAM:
                break;
        }
    }

    private function setLimitTimeRobGoods($skuInfo)
    {
        $shopId = $skuInfo['shop_id'];
        $this->limitRobGoods[$shopId][] = $skuInfo['sku_id'];
    }

    private function setTuanGoods($skuInfo)
    {
        $shopId = $skuInfo['shop_id'];
        $this->tuanGoods[$shopId][] = $skuInfo['sku_id'];
    }

    private function setMealGoods($skuInfo)
    {
        $shopId = $skuInfo['shop_id'];
        $skuId = $skuInfo['sku_id'];
        $this->mealGoods[$shopId][$skuInfo['goods_id']][] = [
            'sku_id' => $skuId,
            'ori_price' => $skuInfo['ori_price'],
        ];
    }

    private function setNewCommerGoods($skuInfo)
    {

    }

    private function setWeekGoods($skuInfo)
    {

    }

    private function setPinTuanGoods($skuInfo)
    {

    }

    private function setFullShippingGoods($skuInfo)
    {
        $shopId = $skuInfo['shop_id'];
        $skuId = $skuInfo['sku_id'];
        if (!isset($this->fullShippingGoods[$shopId][$skuInfo['goods_id']]['buy_num'])) {
            $this->fullShippingGoods[$shopId][$skuInfo['goods_id']] = [
                'buy_num' => 0,
                'weight' => 0,
                'goods_amount' => 0,
                'shipping_templ_id' => $skuInfo['goods_info']['shipping_templ_id'],
            ];
        }
        $this->fullShippingGoods[$shopId][$skuInfo['goods_id']]['buy_num'] += $this->skuNumList[$skuId];
        $this->fullShippingGoods[$shopId][$skuInfo['goods_id']]['weight'] += $skuInfo['weight'] * $this->skuNumList[$skuId];
        $this->fullShippingGoods[$shopId][$skuInfo['goods_id']]['goods_amount'] += $skuInfo['actual_price'] * $this->skuNumList[$skuId];
    }

    private function setShopCouponGoods($skuInfo)
    {
        $shopId = $skuInfo['shop_id'];
        $skuId = $skuInfo['sku_id'];
        $this->shopCouponGoods[$shopId][$skuId] = [
            'goods_id' => $skuInfo['goods_id'],
            'buy_price' => $skuInfo['actual_price'],
            'buy_num' => $this->skuNumList[$skuId]
        ];
    }

    private function setHhzCouponGoods($skuInfo)
    {
        $skuId = $skuInfo['sku_id'];
        $this->couponGoods[$skuId] = [
            'goods_id' => $skuInfo['goods_id'],
            'buy_price' => $skuInfo['actual_price'],
            'buy_num' => $this->skuNumList[$skuId]
        ];
    }

    private function setIntegralGoods($skuInfo)
    {
        $shopId = $skuInfo['shop_id'];
        $skuId = $skuInfo['sku_id'];
        $promoType = null;
        $promoId = null;
        if (isset($skuInfo['activity_type'])) {
            $promoType = $skuInfo['activity_type'];
        }
        if (isset($skuInfo['activity_id'])) {
            $promoId = $skuInfo['activity_id'];
        }
        $this->integralGoods[$shopId][$skuId]['amount'] = $skuInfo['actual_price'] * $this->skuNumList[$skuId];
        $this->integralGoods[$shopId][$skuId]['promo_id'] = $promoId;
        $this->integralGoods[$shopId][$skuId]['promo_type'] = $promoType;
    }

    private function setRedPacketGoods($skuInfo)
    {
        $skuId = $skuInfo['sku_id'];
        $promoType = null;
        $promoId = null;
        if (isset($skuInfo['activity_type'])) {
            $promoType = $skuInfo['activity_type'];
        }
        if (isset($skuInfo['activity_id'])) {
            $promoId = $skuInfo['activity_id'];
        }
        $this->redPacketGoods[$skuId]['amount'] = $skuInfo['actual_price'] * $this->skuNumList[$skuId];
        $this->redPacketGoods[$skuId]['promo_id'] = $promoId;
        $this->redPacketGoods[$skuId]['promo_type'] = $promoType;
    }

    private function setGoods($skuInfo)
    {
        $this->setShippingGoods($skuInfo);
        //其他信息
    }

    private function setShippingGoods($skuInfo)
    {
        $shopId = $skuInfo['shop_id'];
        $skuId = $skuInfo['sku_id'];
        if (!isset($this->shippingGoods[$shopId][$skuInfo['goods_id']][$skuId]['buy_num'])) {
            $this->shippingGoods[$shopId][$skuInfo['goods_id']][$skuId] = [
                'buy_num' => 0,
                'weight' => 0,
                'goods_amount' => 0,
                'volume' => 0,
                'shipping_templ_id' => isset($skuInfo['shipping_templ_id']) && $skuInfo['shipping_templ_id'] ? $skuInfo['shipping_templ_id'] : $skuInfo['goods_info']['shipping_templ_id'],
            ];
        }
        $this->shippingGoods[$shopId][$skuInfo['goods_id']][$skuId]['buy_num'] += $this->skuNumList[$skuId];
        $this->shippingGoods[$shopId][$skuInfo['goods_id']][$skuId]['weight'] += $skuInfo['weight'] * $this->skuNumList[$skuId];
        $this->shippingGoods[$shopId][$skuInfo['goods_id']][$skuId]['volume'] += $skuInfo['volume'] * $this->skuNumList[$skuId];
        $this->shippingGoods[$shopId][$skuInfo['goods_id']][$skuId]['goods_amount'] += $skuInfo['actual_price'] * $this->skuNumList[$skuId];
    }


    public function getSkuActivityType($mealId, $activityType)
    {
        $activityType = $activityType ? $activityType : ActivityInfo::ACTIVITY_TYPE_NORMAL;
        if ($mealId) {
            $activityType = ActivityInfo::ACTIVITY_TYPE_MEAL;
        }
        return $activityType;
    }


    public function getSkuDiscountType($appType)
    {
        $discountTypes = self::LIMIT_APPTYPE_CONFIG[$appType][self::LIMIT_DISCOUNT];
        return $discountTypes;
    }

}